/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.GatherBillBean;
import com.minierp.bean.GatherBillItemBean;
import com.minierp.dao.GatherBillDao;
import com.minierp.model.GatherBill;
import com.minierp.model.GatherBillItem;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class GatherBillValidator implements Validator {
	private GatherBillDao gatherBillDao;

	public boolean supports(Class clazz) {
		return GatherBillBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		GatherBillBean bean = (GatherBillBean) obj;
		GatherBill form = bean.getGatherBill();
		boolean hasError = false;
		if (form.getTallySetId() == 0L) {
			hasError = true;
			errors.rejectValue("gatherBill.tallySetId", "", "请选择『当前开单帐套』");
		}

		if (!hasError
				&& (bean.getCodeRule().getGenerateRule().equals("MANUAL") || bean
						.getOldId() != 0L)) {
			if (form.getBillCode() != null
					&& !form.getBillCode().trim().equals("")) {
				GatherBill hasInput = this.gatherBillDao
						.get(form.getBillCode());
				if (hasInput != null && hasInput.getId() != form.getId()) {
					errors.rejectValue("gatherBill.billCode", "", "『编号』已经存在");
					hasError = true;
				}
			} else {
				errors.rejectValue("gatherBill.billCode", "", "请输入『编号』");
				hasError = true;
			}
		}

		if (!hasError) {
			try {
				SimpleDateFormat arg13 = new SimpleDateFormat("yyyy-MM-dd");
				form.setGatherDate(arg13.parse(bean.getGatherDateStr()));
			} catch (Exception arg11) {
				hasError = true;
				errors.rejectValue("gatherDateStr", "",
						"『收货日期』填写错误，请按以下格式输入：2008-01-01");
			}
		}

		boolean arg14 = false;
		BigDecimal zero = new BigDecimal(0);
		if (!hasError) {
			for (int i = 0; i < bean.getItems().size(); ++i) {
				GatherBillItemBean cb = (GatherBillItemBean) bean.getItems()
						.get(i);
				GatherBillItem c = cb.getGatherBillItem();

				try {
					c.setAmount(new BigDecimal(cb.getAmountStr()));
				} catch (Exception arg12) {
					hasError = true;
					errors.rejectValue("items", "", "『数量』只能输入数字");
					break;
				}

				if (c.getAmount().compareTo(zero) != 0) {
					arg14 = true;
				}
			}
		}

		if (!hasError && !arg14) {
			hasError = true;
			errors.rejectValue("items", "", "收货的『数量』不能全部为0");
		}

	}

	public GatherBillDao getGatherBillDao() {
		return this.gatherBillDao;
	}

	public void setGatherBillDao(GatherBillDao gatherBillDao) {
		this.gatherBillDao = gatherBillDao;
	}
}